/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * DictCreator.java
 * Created on January 20, 2008, 12:39 PM
 */

package tictionary.dict;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


/**
 *
 * @author Erol Hira
 */
public class DictCreator {       
    
    private File dictSourceFile;    
    private Scanner scanner;    
    
    public DictCreator(String dictSourceFile) {
        
        this.dictSourceFile = new File("db.txt");
        
        try {
            scanner = new Scanner(this.dictSourceFile);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    //-----------------------------------------------------------------------
    
    public Dict createDict(){
        
        Dict dict = new Dict();                
        
        while(scanner.hasNextLine()){
            
            String line = scanner.nextLine();            
            
            line = Utils.encodeAsUTF8(line);            
            
            StringTokenizer token = new StringTokenizer(line, ":");
            
            
            String word = null;
            String mean = null;
            
            if(token.hasMoreTokens()){                
                word = token.nextToken();
            }
            
            if(token.hasMoreTokens()){
                mean = token.nextToken();
            } else {
                mean = "";
            }
            
            if(word != null){
                dict.addWord(word, mean);
            }            
        }
        
        return dict;
    }
    
}
